export function getRandomArbitrary(min: number, max: number) {
    return Math.random() * (max - min) + min;
}

export function getRandomInt(min: number, max: number) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min; //不含最大值，含最小值
}

export enum Suit {
    Spade = 1,
    Heart,
    Diamond,
    Club,
}

export interface Card {
    rank: number;
    suit: number;
}

export function generateAllCard() {
    const ret = [];
    for (let i = 1; i < 5; i++) {
        for (let j = 2; j < 15; j++) {
            ret.push({
                rank: j,
                suit: i,
            });
        }
    }
    return ret;
}
/**
 * 模拟交叉洗牌
 * @param cards 待洗的牌
 * @returns 洗好的牌
 */
export function shuffle(cards: Card[]) {
    const divider = getRandomInt(26, 20);
    const group1 = cards.slice(0, divider);
    const group2 = cards.slice(divider);
    const group2Length = group2.length;
    let index1 = 0;
    let index2 = 0;
    while (index2 < group2Length) {
        const littleDivider1 = getRandomInt(1, 6);
        const littleDivider2 = getRandomInt(1, 6);
        index1 = index1 + littleDivider1;
        group1.splice(
            index1,
            0,
            ...group2.slice(index2, Math.min(index2 + littleDivider2, group2Length))
        );
        index2 = index2 + littleDivider2;
    }
    return group1
}

export function cutShuffle(cards: Card[]) {
    const start = getRandomInt(5, 25)
    return cards.concat(cards.splice(start, getRandomInt(10, 52 - start - 10)))
}

export function deal(cards: Card[], numOfPeople: number = 6) {
    const ret = new Array(numOfPeople)
    for (let index = 0; index < 2; index++) {
        for (let j = 0; j < ret.length; j++) {
            if (!ret[j]) {
                ret[j] = cards.splice(0, 1)
            } else {
                ret[j] = ret[j].concat(cards.splice(0, 1))
            }
        }
    }
    return ret
}

export function dealPublic(cards: Card[], cardNum: number = 1 ) {
    cards.splice(0, 1)
    return cards.splice(0, cardNum)
}